<template>
  <div class="article-page">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card shadow="never" border="false">
      <!-- 卡片头部 -->
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button @click="openDrawer('add')" icon="el-icon-plus" size="small" type="primary" round>
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 卡片内容 -->
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="stem" label="标题"></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="lickCount" label="点赞"></el-table-column>
        <el-table-column prop="views" label="浏览数"></el-table-column>
        <el-table-column prop="createdAt" label="更新时间"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('preview', row)"></i>
              <i class="el-icon-edit-outline" @click="openDrawer('edit', row)"></i>
              
              <el-popconfirm confirm-button-text='好的' cancel-button-text='不用了' icon="el-icon-info" icon-color="red"
                title="确定删除吗？" @confirm="deleteArticleData(row)">
                <i class="el-icon-delete" slot="reference"></i>
              </el-popconfirm>
            </div>

              


          </template>

        </el-table-column>
      </el-table>
    </el-card>


    <el-drawer :title="drawerTitle" :visible.sync="showDrawer" :direction="'rtl'" size="50%" @close="drawerClose">
      <div v-if="type === 'preview'" class="article-preview">
        <h5>{{ ruleForm.stem }}</h5>
        <div v-html="ruleForm.content"></div>
      </div>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="70px" v-else>
        <el-form-item label="标题" prop="stem">
          <el-input v-model="ruleForm.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="ruleForm.content" @blur="$refs.ruleForm.validateField('content')"></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="drawerClose">取消</el-button>
        </el-form-item>
      </el-form>

    </el-drawer>

    <el-pagination @current-change="handleCurrentChange" :current-page="current" :page-size="10" background
      layout="prev, pager, next" :total="total">
    </el-pagination>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { getArticleList, addArticle, getArticleDetail, deleteArticle, putArticleDetail } from "@/api/article"
export default {
  components: {
    quillEditor
  },
  name: 'article-page',
  data() {
    return {
      tableData: [],
      current: 1,
      total: 0,
      showDrawer: false,
      drawerTitle: '',
      titleMap: {
        'add': "添加面经",
        'edit': '修改面经',
        'preview': '面经预览'
      },
      ruleForm: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ],
        content: [{ required: true, message: '请输入面经内容', trigger: 'blur' }]
      },
      type: '',
      editId: ''
    }
  },
  async created() {
    this.loadingAticleList()
  },
  methods: {
    // 调用获取列表的方法
    async loadingAticleList() {
      const res = await getArticleList(
        {
          current: this.current,
          pageSize: 10
        }
      )
      // console.log(res.data);
      this.tableData = res.data.rows
      this.total = res.data.total
      // console.log(this.total);
    },
    // 分页器
    async handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.loadingAticleList()
    },
    // 打开抽屉
    async openDrawer(type, row) {
      // console.log(type);
      this.type = type
      // console.log(row);
      // if (type === 'add') {
      //   this.drawerTitle = '添加面经'
      // } else if (type === 'edit') {
      //   this.drawerTitle = '修改面经'
      // } else {
      //   this.drawerTitle = '面经预览'
      // }
      this.drawerTitle = this.titleMap[type]
      this.showDrawer = true

      if (type !== 'add') {
        this.editId = row.id
        const res = await getArticleDetail(row.id)
        // console.log(res);
        this.ruleForm = res.data
      }
    },
    // 关闭抽屉
    drawerClose() {
      console.log(111111);
      if (this.type !== 'preview') {
        this.$refs.ruleForm.resetFields()
      } else {
        this.ruleForm.stem = ''
        this.ruleForm.content = ''
      }
      this.showDrawer = false
    },
    // 确认提交
    submit() {
      if (this.type === "add") {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            await addArticle(this.ruleForm)
            this.$message.success("添加成功")
            this.showDrawer = false,
              this.current = 1,
              this.loadingAticleList()
          }
        })
      } else if (this.type === "edit") {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            let obj = {
              id: this.editId,
              stem: this.ruleForm.stem,
              content: this.ruleForm.content
            }
            console.log(obj);
            await putArticleDetail(obj)
            this.$message.success("修改成功")
            this.showDrawer = false,
              this.current = 1,
              this.loadingAticleList()
          }
        })
      }

      // this.loadingAticleList()
    },
    // 删除
    async deleteArticleData(row) {
      console.log(row);
      await deleteArticle(row.id)
      this.$message.success("删除成功")
      this.loadingAticleList()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }

  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;

    >i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.el-breadcrumb {
  margin-top: 10px;
}

.el-form {
  padding-right: 40px;
}

.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}

.el-rate {
  padding: 10px 0;
}

.article-preview {
  padding: 0 40px 40px 40px;

  >h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>